package com.bdcom.util;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JWindow;

import org.springframework.stereotype.Component;


/**
 * 进度窗
 * 
 * @since 20150717
 * @author Guodeqi
 *
 */
@Component
public class ProgressWindow extends JWindow implements ComponentListener{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private int w = 30;
	
	private int h = 30;
	
	private int arcWidth=20;
	
	private int arcHeight=20;
	
	private int startAngle=360;
	
	private int oval_x=(w-arcWidth)/2;
	
	private int oval_y=(h-arcHeight)/2;
	
	private Timer t=new Timer();
	
	private ProgressWindow(){
		this.setSize(w, h);
		this.setAlwaysOnTop(true);
		addComponentListener(this);
	}
	
	public int getStartAngle() {
		return startAngle;
	}

	public void setStartAngle(int startAngle) {
		this.startAngle = startAngle;
	}
	



	@Override
	public void paint(Graphics g) {
		// TODO Auto-generated method stub
		super.paint(g);
		
		Graphics2D g2d = (Graphics2D)g;
		
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		
		g2d.setColor(Color.WHITE);
		
		g2d.drawOval(oval_x-1,oval_y-1,arcWidth+2,arcHeight+2);
		
		g2d.setStroke(new BasicStroke(2.0f));
		
		g2d.setColor(Color.YELLOW);
		
		g2d.drawArc(oval_x,oval_y,arcWidth, arcHeight, startAngle, -180);
		
	}
	
	public static void main(String[] args) {
		
		ProgressWindow w = new ProgressWindow();
		
		w.setVisible(true);
		
		Timer t=new Timer();
		
		t.schedule(new TimerTask() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				w.setStartAngle(w.getStartAngle()-30==0?360:w.getStartAngle()-30);
				
				w.repaint();				
				
			}
		}, 100,100);
		
	}

	@Override
	public void componentResized(ComponentEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void componentMoved(ComponentEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void componentShown(ComponentEvent e) {
		// TODO Auto-generated method stub
		
		t=new Timer();
		
		t.schedule(new TimerTask() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				setStartAngle(getStartAngle()-30==0?360:getStartAngle()-30);
				
				repaint();				
				
			}
		}, 100,100);
	}

	@Override
	public void componentHidden(ComponentEvent e) {
		// TODO Auto-generated method stub
		t.cancel();
	}
	
	
}	
